package dao

import (
	"ams/global"
	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
	"lib/db"
	"lib/structs/ams"
	"lib/structs/tables"
	"time"
)

var DB *gorm.DB

func InitDB() {
	var err error
	DB, err = db.GetDB(global.Conf.DB.Supper, global.Conf.DB.ShowSqlLog, global.Conf.DB.Source, global.Conf.AppFolder)
	if err != nil {
		logrus.Errorf("连接数据库失败：%v", err)
	}
	autoMigrate()
}

func autoMigrate() {
	type vector[T any] []T
	var li = vector[interface{}]{
		&tables.AmsClient{},
		&tables.AmsClientAbnormal{},
		&tables.AmsClientLog{},
		&tables.AmsPolicy{},
		&tables.AmsPlugin{},
		&tables.AmsAsset{},
		&tables.AmsAssetExtend{},
		&tables.AmsAssetType{},
		&tables.AmsAssetCategory{},
		&tables.AmsAssetConsLog{},
	}
	for _, item := range li {
		if !DB.Migrator().HasTable(item) {
			err := DB.AutoMigrate(item)
			if err != nil {
				logrus.Errorf("初始化数据库表结构异常: %v", err)
			}
		}
	}

	initPolicy()
	initPlugin()
	initAsset()
}

// 初始化基础策略，包含最基本的信息采集、客户端同步 等
func initPolicy() {
	li := []tables.AmsPolicy{
		// 客户端同步更新等行为
		// 基础的信息采集
		{101, "采集-CPU信息", "原idp信息", "cpu", "Name,ProcessorId", "macp", "idp -code=1", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{102, "采集-内存信息", "原idp信息", "memory", "SerialNumber", "macp", "idp -code=2", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{103, "采集-硬盘信息", "原idp信息", "disk", "SerialNumber", "macp", "idp -code=3", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{104, "采集-逻辑硬盘信息", "原idp信息", "logicDisk", "", "macp", "idp -code=4", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{105, "采集-主板信息", "原idp信息", "board", "SerialNumber,Product", "macp", "idp -code=5", ams.ClientPluginFolder, 0, false, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{106, "采集-系统概述信息", "原idp信息", "sys", "", "macp", "idp -code=6", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{107, "采集-开机启动项", "原idp信息", "boot", "", "macp", "idp -code=7", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{108, "采集-活跃网卡信息", "原idp信息", "activeMac", "MACAddress,Caption,Name", "macp", "idp -code=8", ams.ClientPluginFolder, 0, true, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
		{109, "采集-服务信息", "原idp信息", "service", "Command", "macp", "idp -code=9", ams.ClientPluginFolder, 0, false, false, false, "day", 1, 2, 0, 0, 1, time.Now().Unix()},
	}

	var num int64
	err := DB.Model(&tables.AmsPolicy{}).Count(&num).Error
	if err != nil {
		logrus.Errorf(err.Error())
		return
	}
	if num == 0 {
		err = DB.Model(&tables.AmsPolicy{}).CreateInBatches(li, len(li)).Error
		if err != nil {
			logrus.Errorf(err.Error())
			return
		}
	}
}

// 初始化基础插件，包含最基本的插件，例如 update、update_main、macp 等
func initPlugin() {
	li := []tables.AmsPlugin{
		{101, "base", "基础插件", tables.AmsPluginSource.Miao, "base.zip", "plugin", "1.0.0", "0af305488d33546d864e871fe7a253b8", "b9664d66d3d976d4667bc547185d534f", false, false, true, time.Now().Unix()},
		{102, "update", "更新插件", tables.AmsPluginSource.Miao, "update.zip", "plugin", "1.0.0", "0af305488d33546d864e871fe7a253b8", "b9664d66d3d976d4667bc547185d534f", false, false, true, time.Now().Unix()},
		{103, "macp", "ams client 集成插件", tables.AmsPluginSource.Miao, "macp.zip", "plugin", "1.0.0", "4371199389ce9a6a9d98f3e19dd615b7", "197a48aaf2fbc7966ae8999f0a36d052", false, false, true, time.Now().Unix()},
	}

	var num int64
	err := DB.Model(&tables.AmsPlugin{}).Count(&num).Error
	if err != nil {
		logrus.Errorf(err.Error())
		return
	}
	if num == 0 {
		err = DB.Model(&tables.AmsPlugin{}).CreateInBatches(li, len(li)).Error
		if err != nil {
			logrus.Errorf(err.Error())
			return
		}
	}
}

// initAsset 初始化几个基础的资产类型
func initAsset() {
	li1 := []tables.AmsAssetType{
		{1, "固定资产", "年末需要清库盘查的资产类型"},
		{2, "无形资产", "专利、合同、知识产权等无实物的资产类型"},
		{3, "耗材", "办公文具等消耗品"},
	}
	var num int64
	err := DB.Model(&tables.AmsAssetType{}).Count(&num).Error
	if err != nil {
		logrus.Errorf(err.Error())
		return
	}
	if num == 0 {
		err = DB.Model(&tables.AmsAssetType{}).CreateInBatches(li1, len(li1)).Error
		if err != nil {
			logrus.Errorf(err.Error())
			return
		}
	}

	li2 := []tables.AmsAssetCategory{
		{1, "办公用品", "日常办公耗材", 0},
		{2, "电脑终端", "电脑、笔记本、服务器等", 0},
		{3, "移动终端", "手机、平板等", 0},
	}

	var num2 int64
	err = DB.Model(&tables.AmsAssetCategory{}).Count(&num2).Error
	if err != nil {
		logrus.Errorf(err.Error())
		return
	}
	if num2 == 0 {
		err = DB.Model(&tables.AmsAssetCategory{}).CreateInBatches(li2, len(li2)).Error
		if err != nil {
			logrus.Errorf(err.Error())
			return
		}
	}
}
